package foo.mystats;

import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import foo.mystats.db.DataTable;
import foo.mystats.db.RowTable;
import foo.mystats.model.DataType;

public class DataListCursorAdapter extends CursorAdapter {

  private LayoutInflater inflater;

  private static final DateFormat FORMAT_DATETIME = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
  private static final DateFormat FORMAT_DATE = DateFormat.getDateInstance(DateFormat.MEDIUM);

  public DataListCursorAdapter(Context context, Cursor cursor, int flags) {
    super(context, cursor, flags);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public void bindView(View view, Context con, Cursor cursor) {
    TextView date = (TextView) view.findViewById(R.id.data_list_item_date);
    date.setText(getDateTimeAsString(con, cursor));

    TextView value = (TextView) view.findViewById(R.id.data_list_item_value);
    value.setText(getValueAsString(con, cursor));
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(R.layout.data_list_item, parent, false);
  }

  /**
   * Date/time is returned as if we are in GMT timezone.
   * <p/>
   * If we have zero unix epoch time in database it must be displayed as 00:00
   * rather then hh:00
   */
  private String getDateTimeAsString(Context con, Cursor cursor) {
    int dateTimeColumnIndex = cursor.getColumnIndexOrThrow(DataTable.COLUMN_DATETIME);
    int isTimeNeededColumnIndex = cursor.getColumnIndexOrThrow(RowTable.COLUMN_IS_TIME_NEEDED);

    boolean isTimeNeeded = cursor.getInt(isTimeNeededColumnIndex) == 1;
    DateFormat df = isTimeNeeded ? FORMAT_DATETIME : FORMAT_DATE;
    df.setTimeZone(TimeZone.getTimeZone("GMT+00"));

    Date dateTime = new Date();
    dateTime.setTime(cursor.getLong(dateTimeColumnIndex) * 1000);

    return df.format(dateTime);
  }

  public static String getValueAsString(Context con, Cursor cursor) {
    int dataTypeColumnIndex = cursor.getColumnIndexOrThrow(RowTable.COLUMN_DATA_TYPE);
    int valueColumnIndex = cursor.getColumnIndexOrThrow(DataTable.COLUMN_VALUE);

    int dataType = cursor.getInt(dataTypeColumnIndex);
    switch (dataType) {
      case DataType.BOOLEAN:
        return cursor.getInt(valueColumnIndex) == 0 ?
            con.getString(R.string.no) :
            con.getString(R.string.yes);
      case DataType.INTEGER:
        return String.valueOf(cursor.getLong(valueColumnIndex));
      case DataType.REAL:
        return String.valueOf(cursor.getDouble(valueColumnIndex));
      case DataType.STRING:
        return cursor.getString(valueColumnIndex);
      case DataType.TIME:
        Time time = new Time();
        time.set(cursor.getLong(valueColumnIndex) * 1000);
        return time.format("%H:%M");
      default:
        return "";
    }
  }
}
